using System;
using System.Collections.Generic;

namespace EntityKit.Runtime
{
    /// <summary>
    /// 可绑定属性类，用于监听值的变化并触发回调
    /// </summary>
    /// <typeparam name="T">属性值的类型</typeparam>
    public class BindableProperty<T>
    {
        private T mValue;

        private event Action<T> mOnValueChanged;

        // 比较器
        private Func<T, T, bool> mComparer;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defaultValue">默认值</param>
        /// <param name="customComparer">自定义比较器</param>
        public BindableProperty(T defaultValue = default, Func<T, T, bool> customComparer = null)
        {
            mValue = defaultValue;
            mComparer = customComparer ?? DefaultComparer();
        }

        private Func<T, T, bool> DefaultComparer()
        {
            return (a, b) => EqualityComparer<T>.Default.Equals(a, b);
        }

        /// <summary>
        /// 获取或设置属性值
        /// </summary>
        public T Value
        {
            get => mValue;
            set
            {
                if (mComparer(value, mValue)) return;

                mValue = value;
                mOnValueChanged?.Invoke(value);
            }
        }

        /// <summary>
        /// 设置自定义比较器
        /// </summary>
        /// <param name="comparer">比较器函数</param>
        /// <returns>当前实例</returns>
        public BindableProperty<T> WithComparer(Func<T, T, bool> comparer)
        {
            mComparer = comparer ?? DefaultComparer();
            return this;
        }

        /// <summary>
        /// 设置值但不触发通知
        /// </summary>
        /// <param name="value">要设置的值</param>
        public void SetValueWithoutNotify(T value)
        {
            mValue = value;
        }

        /// <summary>
        /// 触发值改变通知
        /// </summary>
        public void NotifyValueChanged()
        {
            mOnValueChanged?.Invoke(mValue);
        }

        /// <summary>
        /// 清除所有事件监听器
        /// </summary>
        public void ClearAllEvents()
        {
            mOnValueChanged = null;
        }

        /// <summary>
        /// 添加值变化监听器
        /// </summary>
        /// <param name="onValueChanged">值变化时的回调函数</param>
        /// <param name="triggerImmediately">是否立即触发一次回调</param>
        public void AddListener(Action<T> onValueChanged, bool triggerImmediately = false)
        {
            if (onValueChanged == null) return;

            // 防止重复注册
            mOnValueChanged -= onValueChanged;
            mOnValueChanged += onValueChanged;

            if (triggerImmediately)
            {
                onValueChanged.Invoke(mValue);
            }
        }

        /// <summary>
        /// 移除值变化监听器
        /// </summary>
        /// <param name="onValueChanged">要移除的回调函数</param>
        public void RemoveListener(Action<T> onValueChanged)
        {
            if (onValueChanged != null)
            {
                mOnValueChanged -= onValueChanged;
            }
        }

        public override string ToString() => mValue?.ToString() ?? "null";
    }
}